
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  processquery;

import java.util.Hashtable;
import java.util.Vector;

import User.Uservalidation;

import control.RequestParameter;

import pool.bean.Bean;
import processquery.Add;
import processquery.Query;



import utility.CapitalChar;
import utility.ConvertToLower;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Add implements Query  
{	
	String Object;
	GetIdName gid;
	String FIELD="field";
	String NEWVALUE="newvalue";
	String USERNAME="+911234567891";
	String id="";
	String MASTERNAME="mastername";
	
	public void setObject(String Object) 
	{
		this.Object = Object;
	}

	public void setdatbase() 
	{
		gid = new GetIdName();
		NEWVALUE=gid.getId(NEWVALUE);
		FIELD = gid.getId(FIELD);
	}

	
	public void processQuery() 
	{	
		RecordSearch rs =new RecordSearch();
		Hashtable<Object, Object> Condition =new Hashtable<Object, Object>();
		Condition.put("mid", Object);
		rs.setConditionMap(Condition);
		String line[] = rs.getArrayFromResultSet();
		Condition.clear();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		for(int i=0;i<line.length;i++)
		{
			String split[] = line[i].split("\t");
			String value = split[Column.pv_index-1];
			if(value.equals("0"))
				value = split[Column.vt_index-1];
			
			table.put(gid.getItem(split[Column.pid_index-1]), value);
		}
		table = ConvertToLower.convertHashKey(table);
		Vector<Object> Fieldlist = new Vector<Object>();
		if(table.containsKey("fieldlist"))
		{
			String Fieldlistobject = table.get("fieldlist").toString();
			Condition.clear();
			Condition.put("mid", Fieldlistobject);
			Condition.put("pid", FIELD);
			rs.setConditionMap(Condition);
			String result[] = rs.getArrayFromResultSet();
			for(int i=0;i<result.length;i++)
			{
				String splitresult[] = result[i].split("\t");
				Condition.clear();
				Condition.put("mid", splitresult[Column.pv_index-1]);
				Condition.put("pid", FIELD);
				rs.setConditionMap(Condition);
				String temp [] = rs.getArrayFromResultSet();
				for(int k=0;k<temp.length;k++)
				{
					String splittemp[] = temp[k].split("\t");
					String value = splittemp[Column.pv_index-1];
					if(value.equals("0"))
						value = splittemp[Column.vt_index-1];
					else
						value = gid.getItem(value);
					Fieldlist.add(value);
				}
			}
			String newquery= table.get(gid.getItem(NEWVALUE)).toString();
			query.query.Query query =new query.query.Query();	
			query.initPool();
			query.settypeofreport("Systemgenerate");
			query.setObject(newquery);
			query.setValue(id);
			query.setReportname(newquery);
			query.initializeData();
			query.updateReport();	
			//query.getResult();
			String buffer[]=query.getbuffer();
			String heading[]  = buffer[0].split("\t");
			Hashtable<Object, Object> MasterMap = new Hashtable<Object, Object>();
			Uservalidation user =new Uservalidation();
			String password = user.getPasword(USERNAME);
			for(int i=1;i<buffer.length;i++)
			{
				String splitbuffer[]= buffer[i].split("\t");
				for(int k=0;k<splitbuffer.length;k++)
				{	
					for(int j=0;j<Fieldlist.size();j++)
					{
						if(Fieldlist.get(j).toString().equalsIgnoreCase((heading[k])))
							MasterMap.put(Fieldlist.get(j), splitbuffer[k]);
							//MasterMap.put(heading[k], splitbuffer[k]);
					}
				}
				String request = table.get("tablename").toString();
				request = gid.getItem(request);
				if(request.equals("no$"))
					request = table.get("tablename").toString();
				try {
					Class C = Class.forName("pool.bean."+CapitalChar.makeFirstCharCapital(request)+"_Bean") ;
					Bean beanObject=(Bean)C.newInstance();
					beanObject.initPool();
					RequestParameter req = new RequestParameter();
					req.setProcess("add");
					req.setRequest(request);
					req.setUsername(USERNAME);
					req.setPassword(password);
					beanObject.setReqParam(req);
					beanObject.setHashtable(ConvertmaptoMastermap(MasterMap));		
					beanObject.insert();
				} catch (ClassNotFoundException e)
				{
					e.printStackTrace();
				}
				catch (InstantiationException e) 
				{	
					e.printStackTrace();
				}
				catch (IllegalAccessException e) 
				{
					e.printStackTrace();
				}
			
			}
		}
				
	}
	
	public Hashtable<Object, Object> ConvertmaptoMastermap(Hashtable<Object, Object> table)
	{
		if(table.containsKey(MASTERNAME))
		{
			Hashtable<Object , Object> result = new Hashtable<Object, Object>();
			result.put("name", table.get(MASTERNAME));
			table.remove(MASTERNAME);
			result.putAll(table);
			return result;
		}
		else
			return table;
	}

	
	public static void main(String[] args) 
	{
		Add add=new Add();
		add.setdatbase();
		add.setid("10");
		add.setObject("2600026");
		add.processQuery();
		//add.Init("2600037");
	}
	public void setid(String id) 
	{
		this.id =  id;
	}
}
